package com.example.controller;

import com.example.entity.Comment;
import com.example.entity.Favorite;
import com.example.entity.User;
import com.example.service.CommentService;
import com.example.service.FavoriteService;
import com.example.service.UserService;
import com.example.vo.CommentVO;
import com.example.vo.Reviewer;
import com.example.vo.VideoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author: hx
 * @CreateTime: 2022/3/7 17:02
 * @Description:
 * */
@RestController
@Slf4j
public class UserServiceController {
    @Autowired
    private UserService userService;
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private CommentService commentService;


    //用户评论
    @PostMapping("/user/comment/{userId}/{videoId}")
    public void comments(@PathVariable("userId") Integer userId,@PathVariable("videoId") Integer videoId, @RequestBody Comment comment){
        comment.setUid(userId);
        comment.setVideoId(videoId);
        commentService.insert(comment);
    }


    /**
     * 视频评论
     * @param videoId
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/user/comments")
    public Map<String, Object> comments(@RequestParam("videoId") Integer videoId,
                                 @RequestParam("page") Integer page,
                                 @RequestParam("per_page") Integer rows){
        log.info("视频id{}",videoId);
        log.info("当前页：{}",page);
        log.info("每页大小：{}",rows);
        HashMap<String, Object> result = new HashMap<>();
        //根据视频id分页查询视频下的内容
        Long total_counts=commentService.findByVideoIdCounts(videoId);
        result.put("total_count",total_counts);
        //根据视频id分页获取对应评论信息以及子评论信息
        ArrayList<CommentVO> commentVOList = new ArrayList<>();
        //根据视频id获取父评论信息
        List<Comment> comments =commentService.findByVideoId(videoId,page,rows);
        //遍历父评论信息
        comments.forEach(comment -> {
            //将评论信息转换为commentVo
            CommentVO commentVO = new CommentVO();
            //属性拷贝  commentvo id content createAt
            BeanUtils.copyProperties(comment,commentVO);
            //获取评论者的信息
            Reviewer reviewer = new Reviewer();
            //根据评论用户id查询用户信息
            User user = userService.queryById(comment.getUid());
            //将用户信息 赋值 reviewer对象中  id name avtar
            BeanUtils.copyProperties(user,reviewer);
            //设置评论信息
            commentVO.setReviewer(reviewer);
            //设置子评论内容
            List<Comment> childComments=commentService.findByParentId(comment.getParentId());
            ArrayList<CommentVO> childCommentVos = new ArrayList<>();
            childComments.forEach(commentChild -> {
                CommentVO commentChildVO = new CommentVO();
                BeanUtils.copyProperties(commentChild,commentChildVO);
                //设置用户评论信息
                User userChild = userService.queryById(commentChild.getUid());
                Reviewer reviewerChild = new Reviewer();
                BeanUtils.copyProperties(userChild,reviewerChild);
                commentChildVO.setReviewer(reviewerChild);
                childCommentVos.add(commentVO);
            });
            //设置子评论
            commentVO.setSubComments(childCommentVos);
            //放入list
            commentVOList.add(commentVO);
        });
        result.put("items",commentVOList);
        return result;
    }


    /**
     * 根据用户id返回用户信息
     * @param id
     * @return
     */
    @GetMapping("/userInfo/{id}")
    public User user(@PathVariable("id")Integer id){
        User user = userService.queryById(id);
        log.info("接受到的用户信息{}",user);
        return user;
    }

    /**
     * 判断该用户是否收藏视频
     * @param videoId
     * @param userId
     * @return
     */
    @GetMapping("/userInfo/favorite")
    public Favorite favorite(String videoId,String userId){
        Favorite favorite = favoriteService.queryByvideoIdAndUserId(Integer.valueOf(videoId),Integer.valueOf(userId));
        log.info("当前返回的收藏对象是否为空：{}", ObjectUtils.isEmpty(favorite));
        return favorite;
    }


}
